<?php 

//-- Classe core ::  STATIQUE :: Sert � charger les fonctionnalit�s et a effectuer des routines de base sur le site
class core
{
	public static $sqlWeb;
	public static $sqlWorld;
	public static $sqlAuth;
	public static $visiteur;
	public static $template;
	
	public static function startPHPSession()
	{
		try 
		{
			session_start();
			
		}
		catch (Exception $e)
	    {
	    	echo "<br/>
		 	<div style='background:#f6cad2;color:#fc0836;padding:10px;width:90%;font-family:arial;border:1px solid#ff0000;' >
		 	<pre>>>> Erreur ! Impossible de démarrer une session.<br>
		 	".$e."
		 	</pre></div>
		 	";
		}
	}
	
	public static function closePHPSession()
	{
		try 
		{	//-- on dit que el visiteur bah il degage
			$visiteur = self::getVisiteur();
			$visiteur->setDisconnected();
			
			if(is_array($_SESSION))
			{
				foreach($_SESSION as $sessVar)
				{
					unset($sessVar);
				}
			}
			session_destroy();
			
			//on detruit le cookie
			if (isset($_COOKIE[COOKIE_SESSIONKEY])) //COOKIE_SESSIONKEY defini dans le .conf du site
			{
				setcookie(COOKIE_SESSIONKEY, "YOUR_SESSION_IS_TERMINATED", time() -3600 , "/", ".killingspree.fr");
			
			}
		}
		catch (Exception $e)
	    {
	    	echo "<br/>
		 	<div style='background:#f6cad2;color:#fc0836;padding:10px;width:90%;font-family:arial;border:1px solid#ff0000;' >
		 	<pre>>>> Erreur ! Impossible de fermer la session.<br>
		 	".$e."
		 	</pre></div>
		 	";
		}
	}

	function setProtection($minlevel=1)
	{
		$visiteur = self::getVisiteur();
		if(!$visiteur->isConnected())
		{
			header("Location: authenticate.html");
			exit(0);
		}
		else
		{
			$user= $visiteur->getUser();
			if($user->security < $minlevel)
			{
				header("Location: error-clearance.html");
				exit(0);
			}
		}
	}
	
	function getSubDomain($host = '') 
	{
		if (empty($host)) $host = $_SERVER['HTTP_HOST'];
		return preg_replace('`([\w\.]+)(?:\.[\w]+){2}`i', '$1', $host);
}
	
	
	public static function initWebServerSQL($host, $username, $passwd, $dbname)
	{
		self::$sqlWeb = new mysqli($host, $username, $passwd, $dbname);
	}
	
	public static function getWebSQL()
	{
		return self::$sqlWeb;
	}
	
	public static function initWorldServerSQL($host, $username, $passwd, $dbname)
	{
		self::$sqlWorld= new mysqli($host, $username, $passwd, $dbname,MYSQL_WORLD_PORT);
	}
	
	public static function getWorldSQL()
	{
		return self::$sqlWorld;
	}
	
	public static function initAuthServerSQL($host, $username, $passwd, $dbname)
	{
		self::$sqlAuth= new mysqli($host, $username, $passwd, $dbname, MYSQL_WORLD_PORT);
	}
	
	public static function getAuthSQL()
	{
		return self::$sqlAuth;
	}
	
	public static function getTemplate($tpl =DEFAULT_TPL_PATH )
	{
		if(self::$template == null)
		{
			self::$template = new Template($tpl);
			return self::$template;
		}
		else
		{
			return self::$template;
		}
	}
	
	public static function getClass($className, $path = DEFAULT_CLASS_PATH)
	{
		$file = "class.".$className.".php";
		if(file_exists($path.$file)) {
				require_once($path.$file);
			} 
		else 
		{
		 	echo "<br/>
		 	<div style='background:#f6cad2;color:#fc0836;padding:10px;width:90%;font-family:arial;border:1px solid#ff0000;' >
		 	<pre>>>> Erreur core::getClass() ! Le fichier <font color='#000000';>'".$file."'</font> situé dans le dossier <font color='#000000';>'".$path."'</font> est introuvable.
		 	</pre></div>
		 	";
		}
	}
	
	public static function getFunctionsPackage($packageName, $path = DEFAULT_FUNCTIONS_PATH)
	{
		$file = "functions.".$packageName.".php";
		if(file_exists($path.$file)) {
				require_once($path.$file);
			} 
		else 
		{
		 	echo "<br/>
		 	<div style='background:#f6cad2;color:#fc0836;padding:10px;width:90%;font-family:arial;border:1px solid#ff0000;' >
		 	<pre>>>> Erreur core :: getFunctionsPackage() ! Le fichier <font color='#000000';>'".$file."'</font> situé dans le dossier <font color='#000000';>'".$path."'</font> est introuvable.
		 	</pre></div>
		 	";
		}
	}
	
	public static function getModule($moduleName, $path = DEFAULT_MODULES_PATH)
	{
		$file = "module.".$moduleName.".php";
		if(file_exists($path.$file)) {
				require_once($path.$file);
			} 
		else 
		{
		 	echo "<br/>
		 	<div style='background:#f6cad2;color:#fc0836;padding:10px;width:90%;font-family:arial;border:1px solid#ff0000;' >
		 	<pre>>>> Erreur core :: getModule() ! Le fichier <font color='#000000';>'".$file."'</font> situé dans le dossier <font color='#000000';>'".$path."'</font> est introuvable.
		 	</pre></div>
		 	";
		}
	}
	
	public static function getVisiteur()
	{
		if(!isset(self::$visiteur))
		{
			self::$visiteur = new Visiteur();
			return self::$visiteur;
		}
		else return self::$visiteur;
	}
	
	public static function getSiteConfVal($confOption)
	{
		$sql = self::$sqlWeb;
		$r = $sql->query("SELECT SQL_CACHE `option_value` FROM site_config WHERE site_option = '".$sql->real_escape_string($confOption)."' ");
		if ($r->num_rows == 0) return false;
		else
		{
			return $r->fetch_object()->option_value;
		}
	}
	
	public static function setSiteConfVal($confOption, $value)
	{
		$sql = self::$sqlWeb;
		$r = $sql->query("UPDATE site_config SET option_value = '".$sql->real_escape_string($value)."' WHERE site_option = '".$sql->real_escape_string($confOption)."' ");
		if($r) return true;
		else return false;
	}
	
	public static function getNews()
	{
		$maxnews = self::getSiteConfVal("nb_news_displayed");
		$sqlSite = core::getWebSQL();
		$SQL = $sqlSite->query("SELECT SQL_CACHE * FROM site_news WHERE actif = 1 ORDER BY date_publication DESC LIMIT ".$maxnews."");
		$tab= NULL;
		while($tmp = $SQL->fetch_object())
		{
			$tab[] = $tmp;
		}
		if(is_array($tab))
		{
			return $tab;
		}
		else
		{
			return false;
		}
	}
	
	public static function getTop10Votes()
	{
		$sqlSite = core::getWebSQL();
		$query = "SELECT SQL_CACHE * FROM site_accounts ORDER BY month_votes DESC LIMIT 10";
		$tab= NULL;
		
		$stmt = $sqlSite->query($query);
		if(!$stmt->num_rows) return false;
		else 
		{
			$tab = NULL;
			while($tmp = $stmt->fetch_object())
			{
				//là on va chercher tout ce qu'il faut
				
				$tab[] = $tmp;
			}
			
			return $tab;
		}
	}
	
	public static function getPLAlerts()
	{
	$sqlSite = core::getWebSQL();
		$query = "SELECT SQL_CACHE * FROM site_pl_reports ORDER BY date DESC";
		$tab= NULL;
		
		$stmt = $sqlSite->query($query);
		if(!$stmt->num_rows) return false;
		else 
		{
			$tab = NULL;
			while($tmp = $stmt->fetch_object())
			{
				//là on va chercher tout ce qu'il faut
				
				$tab[] = $tmp;
			}
			
			return $tab;
		}
	}
	public static function getLastMonthVoteWinners()
	{
		$sqlSite = core::getWebSQL();
		$query = "SELECT SQL_CACHE * FROM site_vote_winners ORDER BY id DESC LIMIT 1";
		$stmt = $sqlSite->query($query);
		if(!$stmt->num_rows) return false;
		else 
		{
			$row = $stmt->fetch_object(); //va chercher la liste des gagnatns (suite d'id au format Texte)
			
			$winnerTab = NULL;
			$idTab = explode(",",str_replace(" ", "", $row->winners));
		
			foreach($idTab as $key =>$userid)
			{
				$tmpUser = new User();
				$tt = explode(":",$userid);
				$tmpUser->getBykey($userid);
				$tmpUser->month_votes = $tt[1];
				$winnerTab[] = $tmpUser ;
				
			}
			
			
			return $winnerTab;
		}
	}
	
}


?>